package  
{
	import flash.display.MovieClip;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Data;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.World;
	import flash.utils.getTimer;
	import flash.media.Microphone;
	
	/**
	 * ...
	 * @author Felix Fleiß, Martina Rasch
	 */
	public class Level1 extends World
	{
		private var time:Number = 0;
		private var oldTime:Number = 0;
		private var oldBonusTime:Number = 0;
		private var nextObstacleTime:Number;
		private var nextBonusTime:Number;
		private var screen_width:int;
		private var screen_height:int;
		private var score:Score;
		private var background:Background;
		private var power:Power;
		
		public function Level1(width:int = 800, height:int = 600) 
		{
			screen_width = width;
			screen_height = height;
			screen_width =  Data.readInt("screen_width");
			screen_height = Data.readInt("screen_height");
			nextObstacleTime = Number(Data.readString("obstacle_interval"));
			nextBonusTime = Number(Data.readString("bonustime_interval"));
			
			score = new Score();
			background = new Background();
			power = new Power();
			//add Player to world
			add(background);
			add(new NoteLines());
			var player:Player = new Player(score, background, power);
			add(new PowerLine(player, power));
			add(power);
			add(player);
			add(score);
			for (var i:int = 12; i < 24; i++) {
				var text:Text = new Text(SingingToNote.noteName[i], 0, screen_height - SingingToNote.mapFrequencyToNoteOctaveNorm(SingingToNote.noteFreq[i]) * screen_height);
				addGraphic(text);
			}
			score.startGame();
		}
		
		override public function update():void
		{
			//calls update of all players and obstacles added to the world
			if (!score.isGameOver()) {
				//level dynamic
				if (score.getTime() < 60) {
					nextObstacleTime = Number(Data.readString("obstacle_interval"));
					nextBonusTime = Number(Data.readString("bonustime_interval"))*8;
				}
				if (score.getTime() < 56) {
					nextObstacleTime = Number(Data.readString("obstacle_interval"))*2;
					nextBonusTime = Number(Data.readString("bonustime_interval"));
				}
				if (score.getTime() < 40) {
					nextObstacleTime = Number(Data.readString("obstacle_interval"));
					nextBonusTime = Number(Data.readString("bonustime_interval"));
				}
				if (score.getTime() < 15) {
					nextObstacleTime = Number(Data.readString("obstacle_interval"))/2;
					nextBonusTime = Number(Data.readString("player_bonustime"));
				}
				super.update();
				time += FP.elapsed;
				if (time - oldTime >= nextObstacleTime)				
				{
					add(new Obstacle);
					oldTime = time;
				}
				if (time - oldBonusTime >= nextBonusTime)				
				{
					add(new TimeBonus);
					oldBonusTime = time;
				}
			} else {
				FP.world = new Highscore(screen_width, screen_height, int(score.getScore()));
			}
		}
		
		public function getWidth():int {
			return screen_width;
		}
		
		public function getHeight():int {
			return screen_height;
		}
	}

}